package io.adrainty.bolee.security.configuration;

import cn.hutool.crypto.asymmetric.RSA;
import io.adrainty.bolee.security.models.properties.SecurityProperties;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author AdRainty
 * @version V1.0.0
 * @since 2025/8/3 上午1:23
 */

@Slf4j
@Configuration
@EnableConfigurationProperties({
        SecurityProperties.class
})
public class SecurityConfig {

    @Bean
    public RSA rsa(SecurityProperties properties) {
        if (StringUtils.isAnyBlank(properties.getPrivateKey(), properties.getPublicKey())) {
            log.warn("未配置RSA密钥对，将使用默认密钥对");
            RSA rsaData = new RSA();
            log.info("生成默认RSA密钥对, publicKey: {}, privateKey: {}",
                    rsaData.getPublicKeyBase64(), rsaData.getPrivateKeyBase64());
            return rsaData;
        }
        return new RSA(properties.getPrivateKey(), properties.getPublicKey());
    }

    public static void main(String[] args) {
        RSA rsaData = new RSA();
        log.info("生成默认RSA密钥对, publicKey: {}, privateKey: {}",
                rsaData.getPublicKeyBase64(), rsaData.getPrivateKeyBase64());
    }

}
